/*
 * Project: BoboDesignPattern
 * 
 * File Created at 2020年5月3日
 * 
 * Copyright 2012 Greenline.com Corporation Limited.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * Greenline Company. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Greenline.com.
 */
package test.studyjdk.thread;

import java.util.concurrent.TimeUnit;

/**
 * @Type RabbitTortoiseGame
 * @Desc
 * @author 52489
 * @date 2020年5月3日
 * @Version V1.0
 */
public class RabbitTortoiseGame implements Runnable {
    boolean winner = false;

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Runnable#run()
     */
    @Override
    public void run() {
        for (int i = 0; i <= 100; i++) {
            System.out.println(Thread.currentThread().getName() + "：跑了" + i + "步");
            if (Thread.currentThread().getName().equals("兔子")) {
                if (i == 80) {
                    try {
                        TimeUnit.SECONDS.sleep(4);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
                try {
                    Thread.sleep(11);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            } else {
                try {
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }
            if (winner) {
                break;
            }
            if (i == 100) {
                winner = true;
                System.out.println(Thread.currentThread().getName() + "赢了");
            }
        }
    }

    public static void main(String[] args) {
        RabbitTortoiseGame game = new RabbitTortoiseGame();
        new Thread(game, "乌龟").start();
        new Thread(game, "兔子").start();
    }

}
